using System.Collections.ObjectModel;

namespace FileTransferDesktop.ViewModels.Pages
{
    public partial class WinFormsIntegrationViewModel : ObservableObject
    {
        [ObservableProperty]
        private string _statusMessage = "准备就绪";

        [ObservableProperty]
        private bool _isConnected = true;

        [ObservableProperty]
        private string _messageToSend = string.Empty;

        [ObservableProperty]
        private ObservableCollection<string> _communicationLog = new();

        public WinFormsIntegrationViewModel()
        {
            // Initialize with some sample log entries
            CommunicationLog.Add($"{DateTime.Now:HH:mm:ss} - WinForms 集成页面已初始化");
            CommunicationLog.Add($"{DateTime.Now:HH:mm:ss} - 等待用户交互...");
        }

        [RelayCommand]
        private void SendMessage()
        {
            if (!string.IsNullOrWhiteSpace(MessageToSend))
            {
                var logEntry = $"{DateTime.Now:HH:mm:ss} - 发送消息: {MessageToSend}";
                CommunicationLog.Add(logEntry);
                StatusMessage = "消息已发送";

                // Clear the message
                MessageToSend = string.Empty;
            }
            else
            {
                StatusMessage = "请输入要发送的消息";
            }
        }

        [RelayCommand]
        private void RefreshData()
        {
            var logEntry = $"{DateTime.Now:HH:mm:ss} - 数据已刷新";
            CommunicationLog.Add(logEntry);
            StatusMessage = "数据已刷新";
        }

        [RelayCommand]
        private void ClearLog()
        {
            CommunicationLog.Clear();
            CommunicationLog.Add($"{DateTime.Now:HH:mm:ss} - 日志已清空");
            StatusMessage = "日志已清空";
        }

        [RelayCommand]
        private void TestConnection()
        {
            IsConnected = !IsConnected;
            var status = IsConnected ? "已连接" : "已断开";
            var logEntry = $"{DateTime.Now:HH:mm:ss} - 连接状态: {status}";
            CommunicationLog.Add(logEntry);
            StatusMessage = $"连接状态已更新: {status}";
        }

        public void LogWinFormsEvent(string eventDescription)
        {
            var logEntry = $"{DateTime.Now:HH:mm:ss} - WinForms事件: {eventDescription}";
            CommunicationLog.Add(logEntry);
            StatusMessage = "收到 WinForms 事件";
        }

        public void UpdateConnectionStatus(bool connected)
        {
            IsConnected = connected;
            var status = connected ? "连接正常" : "连接断开";
            StatusMessage = status;
        }
    }
}
